{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _virtualinstruments:\n",
    "\n",
    "|\n",
    "|\n",
    "\n",
    "Download This Notebook: :download:`VirtualInstruments.ipynb`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Virtual Instruments\n",
    "    \n",
    "## Introduction"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. warning::\n",
    "\n",
    "    The `vi` modules depends on `PyVISA\n",
    "    <https://pyvisa.readthedocs.io/en/latest/>`_ as well as some additional\n",
    "    libraries. Make sure you've followed the instructions in the `installation\n",
    "    tutorial <Installation.ipynb>`_ to ensure a working setup. \n",
    "\n",
    "    Additionally, PyVISA can use a number of different backends to communicate\n",
    "    with instruments. The installation of these is up to the user, but there are\n",
    "    further instructions in the `PyVISA documentation <https://pyvisa.readthedocs.io>`_\n",
    "    By default, PyVISA uses it's Python VISA implementation, but certain instrument\n",
    "    configurations require their manufacturer's specific VISA library be\n",
    "    installed and used to communicate with instruments. For example, the\n",
    "    Keysight IO libraries suite has to be used to communicate with their\n",
    "    instruments over GPIB."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The virtual instrument module defines a consistent high-level interface for\n",
    "interacting with different types of instruments that can be controlled with SCPI\n",
    "(or other means). Primarily for scikit-rf, this is vector network analyzers\n",
    "(VNAs), but we are actively working on adding support for spectrum analyzers.\n",
    "\n",
    "For information on specific instruments, consult the [vi](../api/vi/index.rst)\n",
    "module's documentation.\n",
    "\n",
    "The currently supported instruments are (organized by manufacturer):\n",
    "\n",
    "### Keysight\n",
    "- [FieldFox](../api/vi/generated/skrf.vi.vna.keysight.FieldFox.rst)\n",
    "- [PNA](../api/vi/generated/skrf.vi.vna.keysight.PNA.rst)\n",
    "\n",
    "### NanoVNA\n",
    "- [NanoVNA V2](../api/vi/generated/skrf.vi.vna.nanovna.NanoVNAv2.rst)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. tip::\n",
    "\n",
    "    You can help grow scikit-rf by adding support for more instruments! It's\n",
    "    really not that complicated. Check out the `vi module documentation <../api/vi/index.rst>`_ to\n",
    "    learn how."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Examples"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. note::\n",
    "\n",
    "    You can download this Jupyter notebook and run it directly on your PC:\n",
    "    :download:`VirtualInstruments.ipynb`\n",
    "\n",
    "    Make sure to comment out any code that is not needed for your instrument."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "### Connecting to an instrument"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "from skrf import Frequency\n",
    "from skrf.vi.vna.keysight import PNA\n",
    "from skrf.vi.vna.nanovna import NanoVNAv2\n",
    "\n",
    "# Keysight PNA over ethernet\n",
    "instr = PNA(address=\"TCPIP0::10.0.0.5::INSTR\")\n",
    "ch1 = instr.ch1\n",
    "\n",
    "# NanoVNA V2 over USB\n",
    "instr = NanoVNAv2(\"ASRL/dev/ttyACM0::INSTR\")  # Linux\n",
    "instr = NanoVNAv2(\"ASRL1::INSTR\")  # Windows\n",
    "ch1 = instr  # the NanoVNA does not support channels\n",
    "\n",
    "print(instr.id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Changing the frequency settings of an instrument"
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "freq = Frequency(start=1, stop=2, npoints = 101, unit=\"GHz\")  # create frequency object\n",
    "ch1.frequency = freq  # apply frequency"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "### Creating measurements on the instrument"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# not supported on NanoVNA\n",
    "ch1.create_measurement(name=\"Temp S11\", parameter=\"S11\")\n",
    "ch1.create_measurement(name=\"Temp S22\", parameter=\"S22\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "### Getting a 2-port network and plotting"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "ntwk = ch1.get_snp_network(ports=(1,2))\n",
    "ntwk.plot_s_db()"
   ]
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### Deleting measurements on the instrument"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# not supported on NanoVNA\n",
    "ch1.delete_measurement(name=\"Temp S11\")\n",
    "ch1.delete_measurement(name=\"Temp S22\")"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Raw Cell Format",
  "kernelspec": {
   "display_name": "Python 3.10.4 ('skrf')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "vscode": {
   "interpreter": {
    "hash": "6e9ab46c0308d25f8ecf2297d605bcf30c0184a06f23fc8ad30aef47f26c08c0"
   }
  },
  "nbsphinx": {
   "execute": "never"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
